package com.yxw.yx_hotel.controller;

//import com.sun.deploy.net.URLEncoder;
import java.net.URLEncoder;
import com.yxw.yx_hotel.dto.AddStayWaitDto;
import com.yxw.yx_hotel.dto.QueryStayRecordDto;
import com.yxw.yx_hotel.dto.QueryStayWaitDto;
import com.yxw.yx_hotel.dto.AddStayInDTO;
import com.yxw.yx_hotel.excel.ExportStayWaitExcel;
import com.yxw.yx_hotel.service.StayWaitService;
import com.yxw.yx_hotel.utils.EasypoiExcelUtil;
import com.yxw.yx_hotel.vo.StayRecordVo;
import com.yxw.yx_hotel.vo.StayWaitVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *  待住管理
 * @author liuxin
 * @date 2024.01.18
 */
@RestController
@Api(value = "待住管理", tags = "待住管理")
@Slf4j
@RequestMapping("/api/stayWait")
public class StayWaitController {

    @Autowired
    private StayWaitService stayWaitService;


    @ApiOperation(value = "新增待住名单信息")
    @PostMapping("/addStayWait")
    public BaseResult addStayWait(@Validated @RequestBody AddStayWaitDto dto)  {
        stayWaitService.addStayWait(dto);
        return BaseResult.doSuccess(null,"新增成功");
    }

    @ApiOperation(value = "查询待住列表")
    @PostMapping("/pageStayWaitList")
    public BaseResult<PageVO<StayWaitVo>> pageStayWaitList(@Validated @RequestBody QueryStayWaitDto dto)  {
        PageVO<StayWaitVo> pageVo=stayWaitService.pageStayWaitList(dto);
        return BaseResult.doSuccess(pageVo,"查询成功");
    }

    @ApiOperation(value = "查询待住详情")
    @GetMapping("/getStayWaitById")
    public BaseResult<StayWaitVo> getStayWaitById(@RequestParam Long stayWaitId)  {
        StayWaitVo vo=stayWaitService.getStayWaitById(stayWaitId);
        return BaseResult.doSuccess(vo,"查询成功");
    }

    @ApiOperation(value = "更新待住信息")
    @PostMapping("/updateStayWait")
    public BaseResult updateStayWait(@Validated @RequestBody AddStayWaitDto dto)  {
        stayWaitService.updateStayWait(dto);
        return BaseResult.doSuccess(null,"更新成功");

    }
    @ApiOperation(value = "办理入住")
    @PostMapping("/addStayIn")
    public BaseResult addStayIn(@Validated @RequestBody AddStayInDTO addStayInDTO)  {
        stayWaitService.addStayIn(addStayInDTO);
        return BaseResult.doSuccess(null,"入住成功");

    }

    @ApiOperation(value = "取消待住")
    @PostMapping("/cancelStayWait")
    public BaseResult cancelStayWait( @RequestBody  List<Long> stayWaitIds)  {
        stayWaitService.cancelStayWait(stayWaitIds);
        return BaseResult.doSuccess(null,"取消入住成功");

    }
    @ApiOperation(value = "查询入住历史(记录)列表")
    @PostMapping("/pageStayRecordList")
    public BaseResult<PageVO<StayRecordVo>> pageStayRecordList(@RequestBody QueryStayRecordDto dto)  {
        PageVO<StayRecordVo> pageVO=stayWaitService.pageStayRecordList(dto);
        return BaseResult.doSuccess(pageVO,"查询成功");
    }
    //----------------------------------------------------------------导入导出------------------------------------------------

    @ApiOperation(value = "待住数据导出")
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response,@RequestBody QueryStayWaitDto dto) {
        try {
            List<StayWaitVo> voList = stayWaitService.getStayWaitList(dto);
            EasypoiExcelUtil.exportExcel(voList,"待住人员信息","待住人员信息",StayWaitVo.class,URLEncoder.encode("导出待住信息.xlsx", "UTF-8"),true,response);
            log.info("导出数据成功,总共导出{}条数据",null==voList?0:voList.size());
        }catch (Exception e){
            log.error("导出失败{}",e);
            throw new BaseException(e.getMessage());
        }
    }

    @ApiOperation(value = "模板下载")
    @GetMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) {
        try {
            //模板路径
            String url="templates/importStayWaitTmp.xlsx";
            //导出模板名称
            String fileName="待住人员导入模板.xlsx";
            EasypoiExcelUtil.downTemplate(response,url,fileName);
            log.info("模板下载成功");
        }catch (Exception e){
            log.error("模板导出失败----------------------------------->{}",e);
            throw new BaseException(e.getMessage());
        }
    }

    @ApiOperation(value = "待住数据导入")
    @PostMapping("/importData")
    public BaseResult importData(HttpServletResponse response,@RequestParam("file") MultipartFile file) {
        try {
            List<ExportStayWaitExcel> dataList = EasypoiExcelUtil.importExcel(file, 1, 1, ExportStayWaitExcel.class);
            if(null==dataList || dataList.isEmpty()){
                throw new BaseException("导入数据为空");
            }
            //导入待住数据
            BaseResult result = stayWaitService.importData(dataList);
            return result;
        }catch (Exception e){
            log.error("导入数据失败------------------------------>{}",e);
            throw new BaseException(e.getMessage());
        }
    }
}
